package com.dayouzi.crawler_monitor.controller;

import com.dayouzi.crawler_monitor.base.result.R;
import com.dayouzi.crawler_monitor.config.DataSource;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.query.CrawlerDetailQuery;
import com.dayouzi.crawler_monitor.pojo.query.CrawlerListQuery;
import com.dayouzi.crawler_monitor.pojo.query.ErrorTypeListQuery;
import com.dayouzi.crawler_monitor.pojo.query.HonorListQuery;
import com.dayouzi.crawler_monitor.pojo.vo.*;
import com.dayouzi.crawler_monitor.service.CrawlerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(description = "爬虫管理")
@RestController
@RequestMapping("/crawler/web")
@Slf4j
public class CrawlerController {

    @Autowired
    private CrawlerService crawlerService;


    /**
     * 分页查询爬虫信息list
     * @param page
     * @param limit
     * @param crawlerListQuery
     * @return
     */
    @ApiOperation("分页查询爬虫信息list")
    @PostMapping("/listPage/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestBody CrawlerListQuery crawlerListQuery
    ) {
        PageBean<CrawlerListVO> result = crawlerService.listPage(page, limit, crawlerListQuery);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取负责人列表")
    @GetMapping("/getPersonList")
    public R getPersonList() {
        ArrayList<HashMap<String, String>> result = crawlerService.getPersonList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取一键忽略list")
    @GetMapping("/getDropdownList")
    public R getDropdownList(
            @ApiParam(value = "id", required = true) @RequestParam Integer id
    ) {
        ArrayList<HashMap<String, String>> result = crawlerService.getDropdownList(id);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取爬虫类型列表")
    @GetMapping("/getSpiderTypeList")
    public R getSpiderTypeList() {
        ArrayList<HashMap<String, Integer>> result = crawlerService.getSpiderTypeList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取服务器列表")
    @GetMapping("/getMachineList")
    public R getMachineList(
            @ApiParam(value = "负责人", required = true) @RequestParam("person") String person,
            @ApiParam(value = "爬虫类型", required = true) @RequestParam("spiderType") Integer spiderType
    ) {
        ArrayList<HashMap<String, String>> result = crawlerService.getMachineList(person, spiderType);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取项目名称列表")
    @GetMapping("/getProjectNameList")
    public R getProjectNameList(
            @ApiParam(value = "负责人", required = true) @RequestParam("person") String person,
            @ApiParam(value = "爬虫类型", required = true) @RequestParam("spiderType") Integer spiderType
    ) {
        ArrayList<HashMap<String, String>> result = crawlerService.getProjectNameList(person, spiderType);
        return R.ok().data("result", result);
    }

    @ApiOperation("统计错误类型及数量")
    @GetMapping("/getCrawlerPreview/{id}")
    public R getCrawlerPreview(
            @ApiParam(value = "爬虫id", required = true) @PathVariable Integer id
            ) {
        ArrayList<CrawlerPreviewVO> result = crawlerService.getCrawlerPreview(id);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取爬虫详情")
    @PostMapping("/crawlerDetail/{page}/{limit}")
    public R getCrawlerDetail(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询条件", required = true) @RequestBody CrawlerDetailQuery crawlerDetailQuery
    ) {
        CrawlerDetailVO result = crawlerService.getCrawlerDetail(page, limit, crawlerDetailQuery);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取爬虫详情错误列表")
    @PostMapping("/crawlerDetail/types")
    public R getCrawlerDetailTypes(
            @ApiParam(value = "formData", required = true) @RequestBody ErrorTypeListQuery formData
            ) {
        ArrayList<HashMap<String, String>> result = crawlerService.getCrawlerDetailTypes(formData);
        return R.ok().data("result", result);
    }

    @ApiOperation("修改负责人")
    @GetMapping("/updatePerson")
    public R updatePerson(
            @ApiParam(value = "爬虫id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "负责人", required = true) @RequestParam("person") String person
    ) {
        crawlerService.updatePerson(id, person);
        return R.ok();
    }

    @ApiOperation("修改备注")
    @GetMapping("/updateRemarks")
    public R updateRemarks(
            @ApiParam(value = "爬虫id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "备注", required = true) @RequestParam("remarks") String remarks
    ) {
        crawlerService.updateRemarks(id, remarks);
        return R.ok();
    }

    @ApiOperation("绑定定时任务")
    @GetMapping("/updateScheduledName")
    public R updateScheduledName(
            @ApiParam(value = "爬虫id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "定时任务名称", required = true) @RequestParam("scheduledName") String scheduledName,
            @ApiParam(value = "服务器", required = true) @RequestParam("machine") String machine
    ) {
        Integer scheduledId = crawlerService.updateScheduledName(id, scheduledName, machine);
        return R.ok().data("scheduledId", scheduledId);
    }

    @ApiOperation("解绑定时任务")
    @GetMapping("/unupdateScheduledName")
    public R unupdateScheduledName(
            @ApiParam(value = "爬虫id", required = true) @RequestParam("id") Integer id
    ) {
        crawlerService.unupdateScheduledName(id);
        return R.ok().message("修改成功");
    }

    @ApiOperation("批量处理")
    @PostMapping("/multipleHandle")
    public R multipleHandle(
            @ApiParam(value = "status", required = true) @RequestParam("status") Integer status,
            @ApiParam(value = "arr", required = true) @RequestParam("arr") List arr
    ) {
        crawlerService.multipleHandle(status, arr);
        return R.ok();
    }
    
    @ApiOperation("一键忽略")
    @GetMapping("/oneTouchIgnore")
    public R oneTouchIgnore(
            @ApiParam(value = "爬虫id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "忽略类型", required = true) @RequestParam("type") String type
    ) {
        int num = crawlerService.oneTouchIgnore(id, type);
        return R.ok().data("result", num);
    }

    @ApiOperation("置顶数据")
    @PostMapping("/toppingData")
    public R toppingData(
            @ApiParam(value = "userId", required = true) @RequestParam("userId") Integer userId,
            @ApiParam(value = "arr", required = true) @RequestParam("arr") List arr
    ) {
        crawlerService.toppingData(userId, arr);
        return R.ok();
    }

    @ApiOperation("取消置顶")
    @PostMapping("/unToppingData")
    public R unToppingData(
            @ApiParam(value = "userId", required = true) @RequestParam("userId") Integer userId,
            @ApiParam(value = "arr", required = true) @RequestParam("arr") List arr
    ) {
        crawlerService.unToppingData(userId, arr);
        return R.ok();
    }

    @ApiOperation("标记爬虫是否无效")
    @PostMapping("/setValid")
    public R setValid(
            @ApiParam(value = "isValid", required = true) @RequestParam("isValid") Integer isValid,
            @ApiParam(value = "arr", required = true) @RequestParam("arr") List arr
    ) {
        crawlerService.setValid(isValid, arr);
        return R.ok();
    }

    @ApiOperation("获取爬虫数量")
    @GetMapping("/getCrawlerCount")
    public R getCrawlerCount() {
        int count = crawlerService.getCrawlerCount();
        return R.ok().data("count", count);
    }

    @ApiOperation("获取当日爬虫错误数量")
    @GetMapping("/getErrorCrawlerCount")
    public R getErrorCrawlerCount() {
        int count = crawlerService.getErrorCrawlerCount();
        return R.ok().data("count", count);
    }

    @ApiOperation("切换pdf爬虫")
    @GetMapping("/changePdf")
    public R changePdf(
            @ApiParam(value = "id", required = true) @RequestParam("id") Integer id
    ) {
        crawlerService.changePdf(id);
        return R.ok();
    }

    @ApiOperation("切换不是pdf爬虫")
    @GetMapping("/changePdf2")
    public R changePdf2(
            @ApiParam(value = "id", required = true) @RequestParam("id") Integer id
    ) {
        crawlerService.changePdf2(id);
        return R.ok();
    }

    @ApiOperation("获取爬虫定时任务执行情况")
    @GetMapping("/getScheduledTask")
    public R getScheduledTask(
            @ApiParam(value = "负责人", required = true) @RequestParam("person") String person
    ) {
        ArrayList<ScheduledTaskVO> list = crawlerService.getScheduledTask(person);
        return R.ok().data("list", list);
    }

    @ApiOperation("远程联想搜索爬虫名")
    @GetMapping("/querySearchAsync")
    public R querySearchAsync(
            @ApiParam(value = "爬虫名称", required = true) @RequestParam("codeName") String codeName,
            @ApiParam(value = "负责人", required = true) @RequestParam("person") String person,
            @ApiParam(value = "爬虫类型", required = true) @RequestParam("spiderType") Integer spiderType
    ) {
        ArrayList<HashMap> list = crawlerService.querySearchAsync(codeName, person, spiderType);
        return R.ok().data("list", list);
    }

    /**
     * 分页查询爬虫信息list
     * @param page
     * @param limit
     * @param honorListQuery
     * @return
     */
    @ApiOperation("分页查询荣耀解析list")
    @PostMapping("/honorListPage/{page}/{limit}")
    public R honorListPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestBody HonorListQuery honorListQuery
    ) {
        PageBean<HonorListVO> result = crawlerService.honorListPage(page, limit, honorListQuery);
        return R.ok().data("result", result);
    }

    @ApiOperation("荣耀解析更新处理状态")
    @GetMapping("/honorUpdateData")
    public R honorUpdateData(
            @ApiParam(value = "id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "状态", required = true) @RequestParam("status") Integer status
    ) {
        crawlerService.honorUpdateData(id, status);
        return R.ok();
    }

    @ApiOperation("荣耀解析批量处理")
    @PostMapping("/honorMultipleHandle")
    public R honorMultipleHandle(
            @ApiParam(value = "status", required = true) @RequestParam("status") Integer status,
            @ApiParam(value = "arr", required = true) @RequestParam("arr") List arr
    ) {
        crawlerService.honorMultipleHandle(status, arr);
        return R.ok();
    }

    @ApiOperation("获取荣耀解析来源list")
    @GetMapping("/honorAuthorList")
    public R honorAuthorList() {
        ArrayList<HashMap<String, String>> result = crawlerService.honorAuthorList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取荣耀解析奖项list")
    @GetMapping("/honorPrizeList")
    public R honorPrizeList() {
        ArrayList<HashMap<String, String>> result = crawlerService.honorPrizeList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取企业备案详情信息")
    @GetMapping("/getCompanyRecordDetail/{id}")
    public R getCompanyRecordDetail(
            @ApiParam(value = "id", required = true) @PathVariable("id") Integer id
    ) {
        CompanyRecordDetailVO result = crawlerService.getCompanyRecordDetail(id);
        return R.ok().data("result", result);
    }

    @ApiOperation("校验最近数据查询author字段")
    @GetMapping("/verifiedAuthor")
    public R verifiedAuthor(
            @ApiParam(value = "author", required = true) @RequestParam("author") String author
    ) {
        boolean result = crawlerService.verifiedAuthor(author);
        return R.ok().data("isVerified", result);
    }
    
    @ApiOperation("查询当前负责人有效爬虫数量")
    @GetMapping("/getCrawlerValidCount")
    public R getCrawlerValidCount(
            @ApiParam(value = "person", required = true) @RequestParam("person") String person
    ) {
        CrawlerValidCountVO crawlerValidCountVO = crawlerService.getCrawlerValidCount(person);
        return R.ok().data("result", crawlerValidCountVO);
    }

}
